<?php
    require_once("./config.php");
    require_once("./classes/sessao.php");

    class Upload
    {
        /**
        * Lista os materiais teoricos disponiveis para $topico
        * @param $topico Topico para a listagem dos materiais teoricos disponiveis
        */
        public function Lista($topico)
        {
            global $DOCUMENT_ROOT;
            if (!isset($_SESSION['ANAWEB']) || $_SESSION['ANAWEB']->RetornaIdioma() == "PT_BR")
            {
                $msg = new Traducao($DOCUMENT_ROOT."/linguas/linguas.xml", "pt-br");
            }
            else
            {
                $msg = new Traducao($DOCUMENT_ROOT."/linguas/linguas.xml", "en-us");
            }
            $upload_dir = $DOCUMENT_ROOT."/upload/upload_data";
            $upload_metadata = $DOCUMENT_ROOT."/upload/upload_metadata";

            $professor = $_SESSION['ANAWEB']->RetornaProfessor();
            $metadata = $upload_metadata."/".$professor.".xml";

            if (file_exists($metadata))
            {
                $xml = new DOMDocument("1.0","utf-8");
                $xml->preserveWhiteSpace=false;
                if (!@$xml->load($metadata))
                {
                    echo "<p>Erro ao abrir o arquivo de metadados<p>";
                    unlink($upload_file);
                    $pagina->Footer();
                    exit(1);
                }
                $subt = $xml->getelementsbytagname($topico);
                if ($subt->length)
                {
?>

                    <ul>
<?php
                    $arquivos = $subt->item(0)->getelementsbytagname("file");
                    for ($i=0; $i<$arquivos->length; $i++)
                    {
                        $autor = $arquivos->item($i)->getelementsbytagname("author")->item(0)->nodeValue;
                        $desc = $arquivos->item($i)->getelementsbytagname("description")->item(0)->nodeValue;
                        $data = $arquivos->item($i)->getelementsbytagname("date")->item(0)->nodeValue;
                        $link = $arquivos->item($i)->getelementsbytagname("stored")->item(0)->nodeValue;
?>
                        <li>
                        <h2><?php echo $desc ?></h2>
                        <p><?php echo $msg->get("Autor") ?>: <?php echo $autor ?></p>
                        <p><?php echo $msg->get("Enviado_Em") ?>: <?php echo $data ?></p>
                        <p><a href="baixar.php?prof=<?php echo $professor ?>&arq=<?php echo $link ?>" class="button">&nbsp;Download&nbsp;</a></p>
                        </li>
<?php
                    }
?>
                    </ul>
<?php
                }
                else
                {
?>
                    <p><?php echo $msg->get("Material_Nao_Disponivel") ?></p>
<?php
                }
            }
            else
            {
?>
                <p><?php echo $msg->get("Professor_Sem_Material") ?></p>
<?php
            }

        }
    }
?>
